package com.wdh.kjs.activity;

import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.wdh.kjs.Adapter.BankcardAdapter;
import com.wdh.kjs.Adapter.ListViewForScrollView;
import com.wdh.kjs.R;
import com.wdh.kjs.http.Httpurl;
import com.wdh.kjs.model.JsonModel_my;
import com.wdh.kjs.util.DateUtils;
import com.wdh.kjs.util.ToastUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by feifei on 2016/4/22.
 */
public class BankCardActivity extends BaseTopActivity implements View.OnClickListener,PullToRefreshBase.OnRefreshListener{
    private ListViewForScrollView bankcard_list;
    private ScrollView scrollView;
    private BankcardAdapter bankcardAdapter;
    private LinearLayout addBandCard;
    private Intent intent;
    private JsonModel_my jsonModel_my;
    public static String partyId;
    private ListView actualListView;
    private List<HashMap<String, String>> datas=new ArrayList<HashMap<String,String>>();
    private int size;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bankcardlist);
        SharedPreferences share = getSharedPreferences("logininfo", MODE_PRIVATE);
        partyId = share.getString("loginToken", "");
        initview();
        loadData();
    }

    private void initview() {
        initTopBar("银行卡管理");
        bankcard_list = (ListViewForScrollView) findViewById(R.id.bankcard_listview);
        addBandCard = (LinearLayout)findViewById(R.id.addBandCard);
        addBandCard.setOnClickListener(this);
//        bankcard_list.setOnRefreshListener(BankCardActivity.this);
    }

    private void loadData(){
        RequestParams params = new RequestParams();
        params.addBodyParameter("partyId", partyId);

        HttpUtils http = new HttpUtils();
        http.send(HttpRequest.HttpMethod.POST, Httpurl.URL_BANKCARD_LIST, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                Gson gson = new Gson();
                jsonModel_my = gson.fromJson(responseInfo.result, JsonModel_my.class);
//                Log.i("msg","666");
                if (jsonModel_my.success) {
//                    actualListView = bankcard_list.getRefreshableView();
                    size = jsonModel_my.getBankCardList().size();
                    bankcardAdapter=new BankcardAdapter(BankCardActivity.this, initListViewData(),partyId);
                    bankcard_list.setAdapter(bankcardAdapter);
                }
            }
            @Override
            public void onFailure(HttpException error, String msg) {
                ToastUtils.showShort(getApplicationContext(), "网络异常！");
            }
        });

    }

    private List<HashMap<String, String>> initListViewData() {
        datas = new ArrayList<>();
        for (int i = 0; i < jsonModel_my.getBankCardList().size(); i++) {
            HashMap<String, String> hashMap = new HashMap<>();
            switch (jsonModel_my.getBankCardList().get(i).getBankCardNameType()){
                case 0: hashMap.put("bank", "中国农业银行");break;
                case 1: hashMap.put("bank", "中国工商银行");break;
                case 2: hashMap.put("bank", "中国建设银行");break;
                case 3: hashMap.put("bank", "民生银行");break;
                case 4: hashMap.put("bank", "兴业银行");break;
                case 5: hashMap.put("bank", "交通银行");break;
                case 6: hashMap.put("bank", "中信银行");break;
                case 7: hashMap.put("bank", "平安银行");break;
                case 8: hashMap.put("bank", "广发银行");break;
                case 9: hashMap.put("bank", "招商银行");break;
                case 10: hashMap.put("bank", "中国银行");break;
                case 11: hashMap.put("bank", "浦发银行");break;
                case 12:hashMap.put("bank", "福建海峡银行");break;
                case 13:hashMap.put("bank", "光大银行");break;
                case 14:hashMap.put("bank", "恒丰银行");break;
                case 15:hashMap.put("bank", "华夏银行");break;
                case 16:hashMap.put("bank", "汇丰银行");break;
                case 17:hashMap.put("bank", "泉州银行");break;
                case 18:hashMap.put("bank", "厦门银行");break;
                case 19:hashMap.put("bank", "厦门国际银行");break;
                case 20:hashMap.put("bank", "上海银行");break;
                case 21:hashMap.put("bank", "香港恒生银行");break;
                case 22:hashMap.put("bank", "邮政银行");break;
                case 23:hashMap.put("bank", "浙商银行");break;


                default:break;
            }
            hashMap.put("bankcard_number", jsonModel_my.getBankCardList().get(i).getBankCardNum().substring(jsonModel_my.getBankCardList().get(i).getBankCardNum().length()-4,jsonModel_my.getBankCardList().get(i).getBankCardNum().length()));
            hashMap.put("bankcardId", jsonModel_my.getBankCardList().get(i).getBankCardId());
            hashMap.put("isMainCard", jsonModel_my.getBankCardList().get(i).getIsMainTradeCard());
            hashMap.put("bandCard_size", jsonModel_my.getBankCardList().size()+"");
            Log.i("msg","是否是主卡  "+jsonModel_my.getBankCardList().get(i).getIsMainTradeCard());

            datas.add(hashMap);
        }
        return datas;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.addBandCard:
                if (size==0) {
                    intent = new Intent(this, AddBankCardActivity.class);
                    startActivity(intent);
                }else {
                    TipDialogShow("只能添加一张银行卡");
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onRefresh(PullToRefreshBase refreshView) {
        {
            String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(),
                    DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);

            // Update the LastUpdatedLabel
            refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);

            // Do work to refresh the list here.
            new GetDataTask().execute();
        }
    }
    private class GetDataTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            loadData();
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            bankcardAdapter.notifyDataSetChanged();
//            bankcard_list.onRefreshComplete();
            super.onPostExecute(aVoid);
        }
    }
    private void TipDialogShow(String hint) {
        View dialog = View.inflate(this,R.layout.view_dialog_tip, null);
        Button bt_dialog_false_positive = (Button) dialog.findViewById(R.id.bt_dialog_false_positive);
        TextView tv_dialog_false_hint = (TextView) dialog.findViewById(R.id.tv_dialog_false_hint);
        tv_dialog_false_hint.setText(hint);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(dialog);
        builder.create();
        final AlertDialog dialogShow = builder.show();
        bt_dialog_false_positive.setOnClickListener(new android.view.View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dialogShow.dismiss();
            }
        });
    }

}
